package com.sto.stoapp.widget;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.LogoutBroad;

public class StoHomeActivity extends BaseActivity {

	private LogoutBroad logoutBroad;
	private String homeAction="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public void setTitleBottom(int id, final String action) {
		register(action);
		try {
			this.setStoTitle(id);
			findViewById(R.id.home).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					//System.out.println("***setTitleBottom***");
					exitToMenu();
				}
			});

			findViewById(R.id.back).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					//Add by wj 修改e递宝无法关闭扫描头bug
					setSaveData(true);
					if (StoApplication.getStoJni() != null) {
						StoApplication.getStoJni().SetScannerStop();
					}
					showNotice();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setTitleBottom(String title, final String action) {
		register(action);
		try {
			this.setStoTitle(title);
			findViewById(R.id.home).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					//System.out.println("***setTitleBottom***");
					exitToMenu();
				}
			});

			findViewById(R.id.back).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					finish();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void exitToMenu(){
		Intent intent = new Intent(homeAction);
		sendBroadcast(intent);
	}

	public void register(String action) {
		homeAction = action;
		IntentFilter filter = new IntentFilter(action);
		logoutBroad = new LogoutBroad();
		registerReceiver(logoutBroad, filter);
	}
	private void unRegister() {
		if (logoutBroad != null) {
			unregisterReceiver(logoutBroad);
		}
	}

	@Override
	protected void onDestroy() {
		unRegister();
		System.out.println("StoScanActivity : StoHomeActivity onDestroy");
		super.onDestroy();
		
	}
	
}
